﻿// Copyright 2012 T4C: sejerpz@gmail.com
// Licensed under GPL v3

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SET.Core
{
    static class StringExtensions
    {
        public static string ToValidIdentifier(this string data)
        {
            var builder = new StringBuilder();
            bool nextUpper = true;

            foreach(char ch in data)
            {
                if (char.IsWhiteSpace(ch))
                {
                    nextUpper = true;
                }
                else if (ch == '{')
                {
                    builder.Append('_');
                }
                else if (!char.IsLetterOrDigit(ch))
                {
                    //builder.Append("_");
                    nextUpper = true;
                }
                else
                {
                    if (nextUpper)
                    {
                        builder.Append(ch.ToString().ToUpper());
                        nextUpper = false;
                    }
                    else
                        builder.Append(ch);
                }
            }

            return builder.ToString();
        }
    }
}
